import Vue from 'vue'


import VueRouter from 'vue-router'
import Home from '@/components/Home'
import Login from '@/components/Login'
import { getCookie } from '@/utils/cookie.js'
// import Notfound from '@/components/Notfound'

Vue.use(VueRouter)
// 该段代码不需要记，理解即可
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};
//引入路由权限接口
import { pressApi } from '@/utils/https'
//引入store实例对象
import store from '@/store/index'
const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
  },

  {
    path: '*',
    name: 'Notfound',
    component: () => import('@/components/Notfound'),
  }, 
  {
    path: '/home',
    // redirect:'/home/student',
    name: 'home',
    component: Home,
    //设置动态图标
    iconClass: 'el-icon-menu',
    children: [
      {
        path: '',
        // redirect:'/home/student',
        name: 'Homelist',
        component: () => import('@/components/view/Homelist'),
        //设置动态图标
        meta:{
          title:'首页',
          icon:'el-icon-s-data'
           
        }

      },
      {
        path: 'user',
        // redirect:'/home/student',
        name: 'User',
        component: () => import('@/components/view/User'),
        //设置动态图标
        meta:{
          title:'客户管理',
          icon:'el-icon-s-custom'
           
        }

      },
      {
        path: 'message',
        // redirect:'/home/student',
        name: 'Message',
        component: () => import('@/components/view/Message'),
        //设置动态图标
        meta:{
          title:'信息管理',
          icon:'el-icon-notebook-1'
           
        },
        children: [
          {
            path: '',
            // redirect:'/home/student',
            name: 'Myinfo',
            component: () => import('@/components/view/Myinfo'),
            //设置动态图标
            meta:{
              title:'影院地址信息',
              icon:'el-icon-s-flag'
               
            },

          },
        ]
      },

      // {
      //   path: 'shenpi',
      //   // redirect:'/home/student',
      //   name: 'shenpi',
      //   component: () => import('@/components/view/Shenpi'),
      //   //设置动态图标
      //   imeta:{
      //     title:'审批管理',
      //     icon:'el-icon-coordinate'
           
      //   }

      // },

      // {
      //   path: 'qingjia',
      //   // redirect:'/home/student',
      //   name: 'qingjia',
      //   component: () => import('@/components/view/Qingjia'),
      //   //设置动态图标
      //   meta:{
      //     title:'请假审批',
      //     icon:'el-icon-s-order'
           
      //   }

      // },

      // {
      //   path: 'woqingjia',
      //   // redirect:'/home/student',
      //   name: 'woqingjia',
      //   component: () => import('@/components/view/Woqingjia'),
      //   //设置动态图标
      //   meta:{
      //     title:'我要请假',
      //     icon:'el-icon-headset'
           
      //   }

      // },


    ]
  },

]
//创建路由实例对象
const router = new VueRouter({
  mode: 'history',
  routes
})

router.beforeEach((to, from, next) => {
  let token = getCookie('token')
  if (token) {
    //请求左侧餐单栏数据
    if(to.path=='/login'){
      //手动修改UL,跳转登录时,如果有token调回首页
      next('/home')
    }else{
      let role = localStorage.getItem('role')
    if(store.state.routes.length==0){

    pressApi({ role: role }).then(res => {
      // console.log(res);
      res.data.forEach(item => {
        item.component=()=>import(`@/components/view/${item.path}.vue`)
        routes[3].children.push(item)
      });
      // console.log(router);
      //将更新完的存入vuex
      //重新放入路由信息
     
      store.dispatch('setRoutesAsync', routes)
      router.addRoutes(routes)
      next({ ...to, replace: true })
    })
  }else{
    next() 
  }
    }
    
  } else if (!token && to.path != '/login') {
    next('/login')
  } else {
    next()
  }
})


export default router

